﻿using AutoMapper;
using Domain.Material_Management;
using Infratructure.Interface;
using MediatR;
using ReturnValue;

namespace MES.Server.API.Application.Command.Material_Management
{
    public class DelProductMaterialsHander : IRequestHandler<DelProductMaterialsCommand, ApiResult<int>>
    {
        private readonly IRepository<ProductMaterialsModel> productMaterialsRepo;
        private readonly IMapper mapper;
        public DelProductMaterialsHander(IRepository<ProductMaterialsModel> productMaterialsRepo, IMapper mapper)
        {
            this.productMaterialsRepo = productMaterialsRepo;
            this.mapper = mapper;
        }
        /// <summary>
        /// 产品物料删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(DelProductMaterialsCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>();
            if (request.Ids != null && request.Ids.Any())
            {
                var ProductMaterialss = new List<ProductMaterialsModel>();
                var notFoundIds = new List<long>();

                foreach (var id in request.Ids)
                {
                    var ProductMaterials = await productMaterialsRepo.GetValue(id);
                    if (ProductMaterials == null)
                    {
                        notFoundIds.Add(id);
                    }
                    else
                    {
                        ProductMaterials.IsDel = true;
                        ProductMaterialss.Add(ProductMaterials);
                    }
                }

                if (ProductMaterialss.Any())
                {
                    await productMaterialsRepo.UpdateRangeAsync(ProductMaterialss);
                    result.Code = StatusCode.Success;
                    result.Message = $"成功删除{ProductMaterialss.Count}个产品物料";
                    result.Data = ProductMaterialss.Count;

                    if (notFoundIds.Any())
                    {
                        result.Message += $",以下产品物料不存在: {string.Join(",", notFoundIds)}";
                    }
                }
                else
                {
                    result.Code = StatusCode.Fail;
                    result.Message = "没有找到可删除的产品物料信息";
                }
            }
            else
            {
                var productMaterials = await productMaterialsRepo.GetValue(request.Id);
                if (productMaterials == null)
                {
                    result.Code = StatusCode.Fail;
                    result.Message = "产品物料不存在,无法删除";
                    return await Task.FromResult(result);
                }
                else
                {
                    productMaterials.IsDel = true;
                    await productMaterialsRepo.UpdateAsync(productMaterials);
                    result.Code = StatusCode.Success;
                    result.Message = "删除产品物料成功";
                    return await Task.FromResult(result);
                }
            }
            return await Task.FromResult(result);
            
        }
    }
}
